package com.yanboo.modules.mnt.rest;

import com.yanboo.common.annotation.CustomController;
import com.yanboo.modules.mnt.service.ServerDeployService;
import com.yanboo.modules.mnt.service.dto.ServerDeployDto;
import com.yanboo.modules.mnt.service.dto.ServerDeployQueryCriteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import com.yanboo.logging.annotation.Log;
import com.yanboo.modules.mnt.domain.ServerDeploy;
import com.yanboo.common.utils.PageResult;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * 运维：服务器管理
 */
@Tag(name = "运维：服务器管理")
@CustomController
@RequiredArgsConstructor
@RequestMapping("/api/serverDeploy")
public class ServerDeployController {

    private final ServerDeployService serverDeployService;

    /**
     * 导出服务器数据
     * @param response
     * @param criteria
     * @throws IOException
     */
    @Operation(summary = "导出服务器数据")
    @GetMapping(value = "/download")
    public void exportServerDeploy(HttpServletResponse response, ServerDeployQueryCriteria criteria) throws IOException {
        serverDeployService.download(serverDeployService.queryAll(criteria), response);
    }

    /**
     * 查询服务器
     * @param criteria
     * @param pageable
     * @return
     */
    @Operation(summary = "查询服务器")
    @GetMapping
    public ResponseEntity<PageResult<ServerDeployDto>> queryServerDeploy(ServerDeployQueryCriteria criteria, Pageable pageable){
    	return new ResponseEntity<>(serverDeployService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    /**
     * 新增服务器
     * @param resources
     * @return
     */
    @Operation(summary = "新增服务器")
    @Log("新增服务器")
    @PostMapping
    public ResponseEntity<Object> createServerDeploy(@Validated @RequestBody ServerDeploy resources){
        serverDeployService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改服务器
     * @param resources
     * @return
     */
    @Operation(summary = "修改服务器")
    @Log("修改服务器")
    @PutMapping
    public ResponseEntity<Object> updateServerDeploy(@Validated @RequestBody ServerDeploy resources){
        serverDeployService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除Server
     * @param ids
     * @return
     */
    @Operation(summary = "删除Server")
    @Log("删除服务器")
	@DeleteMapping
    public ResponseEntity<Object> deleteServerDeploy(@RequestBody Set<Long> ids){
        serverDeployService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 测试连接服务器
     * @param resources
     * @return
     */
    @Operation(summary = "测试连接服务器")
	@Log("测试连接服务器")
	@PostMapping("/testConnect")
	public ResponseEntity<Object> testConnectServerDeploy(@Validated @RequestBody ServerDeploy resources){
		return new ResponseEntity<>(serverDeployService.testConnect(resources),HttpStatus.CREATED);
	}
}
